package com.xtz.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.xtz.domain.Dish;
import com.xtz.domain.DishFlavor;
import com.xtz.dto.AddDishDto;
import com.xtz.mapper.AdminDishMapper;
import com.xtz.service.AdminDishService;
import com.xtz.vo.DishVo;
import com.xtz.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * date: 2022/5/7 18:54
 *
 * @author xietianze
 */
@Service
public class AdminDishServiceImpl implements AdminDishService {
    @Autowired
    private AdminDishMapper adminDishMapper;

    /**
     * 添加菜品
     *@return
     * @param dto
     */
    @Override
    public Boolean addDish(AddDishDto dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);


        dish.setCreateTime(new Date());
        dish.setUpdateTime(new Date());

        dish.setCreateUser("1");
        dish.setUpdateUser("1");
        dish.setType(1);
        dish.setSort(1);
        return adminDishMapper.addDish(dish)>0;
    }

    /**
     * 添加口味
     *
     * @param dto
     * @return
     */
    @Override
    public boolean addDishFlavor(DishFlavor dto) {
        DishFlavor dishFlavor = new DishFlavor();
        BeanUtils.copyProperties(dto,dishFlavor);
        String s = new Snowflake().nextIdStr();
        dishFlavor.setId(s);
        dishFlavor.setCreateTime(new Date());
        dishFlavor.setUpdateTime(new Date());
        dishFlavor.setCreateUser("1");
        dishFlavor.setUpdateUser("1");
        dishFlavor.setIsDeleted(0);



        return adminDishMapper.addDishFlavor(dishFlavor) > 0;
    }

    /**
     * 根据name分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     */
    @Override
    public List<DishVo> getDishPageByName(Integer page, Integer pageSize, String name,String type) {
        page = (page - 1) * pageSize;
        return adminDishMapper.selectPageByName(page, pageSize, name);
    }

    @Override
    public Long getCountByName(String name) {

        return adminDishMapper.selectCountByName(name);
    }

    /**
     * 查询菜单列表
     *
     * @param condition
     * @return
     */
    @Override
    public List<DishVo> getListByCondition(Map<String, Object> condition) {
        return adminDishMapper.selectByCondition(condition);
    }



    /**
     * 通过dishid查询口味列表
     *
     * @param list
     * @return
     */
    @Override
    public List<DishFlavor> getDishFlavor(List list) {
        return adminDishMapper.selectFlavor(list);
    }

    @Override
    public Result deleteByIds(List<String> ids) {
        int size = ids.size();
        if (size == 0) {
            return new Result(1, "失败", null, null);
        }
        int a = adminDishMapper.deleteDishByIds(ids);
        int b = adminDishMapper.deleteDishFlavorByIds(ids);
        if (a > 0 && b > 0) {
            return new Result(1, "成功", null, null);
        }
        return new Result(1, "失败", null, null);
    }

    @Override
    @Transactional
    public Boolean update(Dish dish) {

        //修改口味
            //删除旧口味
        int i = adminDishMapper.deleteById(dish.getId());
            //添加新口味
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            String idStr = new Snowflake().nextIdStr();
            flavor.setId(idStr);
            flavor.setDishId(dish.getId());
            flavor.setCreateUser("1");
            flavor.setUpdateUser("1");
            flavor.setCreateTime(new Date());
            flavor.setUpdateTime(new Date());
            flavor.setIsDeleted(0);

            adminDishMapper.addDishFlavor(flavor);
        }

        //修改菜品
        dish.setUpdateUser("11");
        dish.setUpdateTime(new Date());
        dish.setSort(1);


        return adminDishMapper.updatadishes(dish) >0;
    }

    @Override
    public List<DishVo> getListByCategoryId(String categoryId) {

        return adminDishMapper.selectByCategoryId(categoryId);
    }

    @Override
    public List<DishVo> getListByTwo(String categoryId, Integer status) {
        return adminDishMapper.selectByTwo(categoryId, status);
    }


    @Override
    public List<DishFlavor> getFlavorByDishId(String dishId) {
        return adminDishMapper.selectByDishId(dishId);
    }

    @Override
    public Dish selectById(String id) {
        if (StrUtil.isNotEmpty(id)) {
            Dish dish = adminDishMapper.selectById(id);
            return dish;
        }
        return adminDishMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Integer status, List<String> id) {
        return adminDishMapper.updateStatus(status, id) > 0;
    }

}
